layui.use(['form', 'layer', 'laydate'], function () {

    var $ = layui.$
        , form = layui.form
        , laydate = layui.laydate;

    //监听 是否共享航班
    form.on('radio(isApplyShareFlight)', function (data) {
        let carryVerifyCabinPriceDiv = $('#carryVerifyCabinPriceDiv');
        let value = data.value;
        if (value == 1) {
            carryVerifyCabinPriceDiv.show();
        } else {
            carryVerifyCabinPriceDiv.hide();
        }
    });

    //监听 是否代订位
    form.on('radio(isAgentBooking)', function (data) {
        let agentBookFailTypeDiv = $('#agentBookFailTypeDiv');
        let seatLimitDiv = $('#seatLimitDiv');
        let value = data.value;
        if (value == 1) {
            agentBookFailTypeDiv.show();
            seatLimitDiv.show();
        } else {
            agentBookFailTypeDiv.hide();
            seatLimitDiv.hide();
        }
    });

    //监听 适用航班号
    form.on('radio(flightNoFit)', function (data) {
        let value = data.value;
        if (value == 0) {
           $('input[name="flightNoList"]').get(0).value = "";
        }
        flightNoList();
    });
    
    function flightNoList() {
        let value = $('input[type="radio"][name="flightNoFit"]:checked').val();
        let checkDiv = $('#flightNoList').get(0);
        if (value == '1' || value == '2') {
            checkDiv.setAttribute('lay-verify', 'required|number');
        } else {
            checkDiv.setAttribute('lay-verify', '');
        }
    }

    //适用航班号校验
    flightNoList();

    //监听 订位失败处理
    form.on('radio(agentBookFailType)', function (data) {
        let agentBookFailTypeTip = $('#agentBookFailTypeTip');
        let value = data.value;
        if (value == 0) {
            agentBookFailTypeTip.show();
        } else {
            agentBookFailTypeTip.hide();
        }
    });


    /**
     * 适用班期 全选
     */
    $('#flight-week-all').on('click', function () {

        $('input[name="weekSegmentList[]"]').each(function () {
            this.checked = true;
        });
        checkDay();

        form.render('checkbox'); //刷新
    });

    /**
     * 适用班期 反选
     */
    $('#flight-week-invert').on('click', function () {

        $('input[name="weekSegmentList[]"]').each(function () {
            this.checked = !this.checked;
        });
        checkDay();

        form.render('checkbox'); //刷新
    })

    //监听适用班期
    form.on('checkbox(flightDay)', function (data) {
        checkDay();
    });

    var checkDay = function checkDays() {
        let divEle = $('#DaysFDiv');
        let checkbox = divEle.find('input[type="checkbox"][name="weekSegmentList[]"]');
        let checkedCount = checkbox.filter(':checked').length;
        let newDivEle = divEle.get(0);
        if (checkedCount == 0) {
            $('#DaysCheck').show();
            newDivEle.setAttribute("lay-verify", "required");
        } else {
            $('#DaysCheck').hide();
            newDivEle.setAttribute("lay-verify", "");
        }
    }
    checkDay();

    //航班开始日期
    laydate.render({
        elem: '#flight-start',
        min: 0,
        shade: [0.1, '#ddd'],
    });

    // 航班结束日期
    laydate.render({
        elem: '#flight-end',
        min: 0,
        shade: [0.1, '#ddd'],
    });

    //销售开始日期
    laydate.render({
        elem: '#sale-start',
        min: 0,
        shade: [0.1, '#ddd'],
    });

    // 销售结束日期
    laydate.render({
        elem: '#sale-end',
        min: 0,
        shade: [0.1, '#ddd'],
    });

    // 销售开始日期 时间插件
    laydate.render({
        elem: '#sale-start-time',
        type: 'time',
        format: 'HH:mm' // 设置时间格式
    });

    // 销售结束日期 时间插件
    laydate.render({
        elem: '#sale-end-time',
        trigger: 'click',
        type: 'time',
        format: 'HH:mm', // 设置时间格式
        done: function (value, date) {
        }
    });

    // N天内 时间插件
    laydate.render({
        elem: '#NDateInTime',
        type: 'time',
        format: 'HH:mm' // 设置时间格式
    });

    // N天外 时间插件
    laydate.render({
        elem: '#NDateOutTime',
        trigger: 'click',
        type: 'time',
        format: 'HH:mm', // 设置时间格式
        done: function (value, date) {
        }
    });


    //监听 库存类型
    form.on('radio(sellType)', function (data) {
        let avEffectiveSaleValueDiv = $('#avEffectiveSaleValueDiv');
        let value = data.value;
        if (value == 1) {
            avEffectiveSaleValueDiv.show();
        } else {
            avEffectiveSaleValueDiv.hide();
        }
    });

    // 展示隐藏 主承运航司
    var checkCarryVerifyCabinPrice = function doCheckCarryVerifyCabinPrice() {
        let value = $('input[name="carryVerifyCabinPrice"]:checked').val();
        let carryAirCompanyDiv = $('#carryAirCompanyDiv');
        let carryAirCompany = $('#carryAirCompany');
        if (value == 1) {
            carryAirCompanyDiv.show();
            carryAirCompany.get(0).setAttribute('lay-verify', 'required|uppercase');
        } else {
            carryAirCompanyDiv.hide();
            carryAirCompany.get(0).setAttribute('lay-verify', '');
        }
    }
    checkCarryVerifyCabinPrice();

    // 监听 是否出主承运
    form.on('radio(carryVerifyCabinPrice)', function (data) {
        checkCarryVerifyCabinPrice();
    });

})